<?php
namespace app\admin\controller;
use app\admin\model\Seek as SeekModel;

//求购管理类
class Seek extends Base
{
    /**
     * 求购列表
     */
    public function seekLst()
    {
        $list = SeekModel::field('sk_del_aid,sk_note,sk_del_time',true)->order('sk_create_time DESC')->paginate(10);

        $data = displayJson($list);

        $arr  = json_decode($data,true);

        $userIds   = array();
        $newArr    = $arr['data'];

        foreach ($newArr as $val) {
            $userIds[] = $val['sk_uid'];
        }

        $user_list = $this->getUserEmailByIds($userIds);

        foreach ($newArr as $key => &$val) {
            $newArr[$key]['us_status'] = $user_list[$val['sk_uid']]['us_status'];
            $newArr[$key]['sk_uid']    = $user_list[$val['sk_uid']]['us_email'];

        }

        $arr['data'] = $newArr;

        $info['code'] = '200';
        $info['data'] = $arr;
        echo displayJson($info);die;
    }

    /**
     * 获取求购详情
     */
    public function seekDesc()
    {
        $sid = input('sid');

        $res = db('seek',[],false)->where('sk_id',$sid)->find();

        if ($res) {
            $uid              = $res['sk_uid'];
            $userInfo         = $this->getUserEmail($uid);
            $res['sk_uid']    = $userInfo[$uid]['us_email'];
            $res['us_status'] = $userInfo[$uid]['us_status'];

            if ($res['sk_del_aid']) {
                $getAdmin = db('admin',[],false)->where('ad_id',$res['sk_del_aid'])->find();

                $res['sk_del_aid'] = $getAdmin['ad_account'];
            }

            $info['code'] = '200';
            $info['data'] = $res;
            echo displayJson($info);die;
        } else {
            $info['code']    = '400';
            $info['message'] = '数据未找到';
            echo displayJson($info);die;
        }
    }

    /**
     * 删除求购
     */
    public function seekDel()
    {
        $sid  = input('sid');//求购id
        $aid  = input('aid'); //管理员id
        $note = htmlspecialchars(input('note'));

        if (empty($sid) || empty($aid)) {
            $info['code']    = '401';
            $info['message'] = '数据异常，终止访问';
            echo displayJson($info);die;
        }

        if (empty($note)) {
            $info['code']    = '400';
            $info['message'] = '删除原因必填';
            echo displayJson($info);die;
        }

        $getSeek = db('seek',[],false)->where('sk_id',$sid)->find();

        if ($getSeek['sk_status'] == 2) {
            $info['code']    = '400';
            $info['message'] = '该求购已被删除，请刷新后查看';
            echo displayJson($info);die;
        }

        $set['sk_status']  = 2;
        $set['sk_del_aid'] = $aid;
        $set['sk_note']    = $note;
        $set['sk_del_time']= time();

        $del = db('seek',[],false)->where('sk_id',$sid)->update($set);

        if ($del) {
            //发送邮件告知用户
            $getUser = db('user',[],false)->where('us_id',$getSeek['sk_uid'])->find();

            $send_data = array(
                'to_email'   =>$getUser['us_email'],
                'content'    =>'【淘二货】您发布的求购二货：<span style="font-size: 20px;font-weight: bold;"> '.$getSeek['sk_title'].' </span>，已被管理员删除。<br/>删除原因： '.$note.'<br/>如有疑问，请联系管理员QQ：1298025852'
            );

            $send      = sendEmail($send_data);

            if (!$send) {
                $info['code']    = '400';
                $info['message'] = '删除成功,发送邮件失败';
                echo displayJson($info);die;
            } else {
                $info['code']    = '200';
                $info['message'] = '删除成功';
                echo displayJson($info);die;
            }
        } else {
            $info['code']    = '400';
            $info['message'] = '删除失败';
            echo displayJson($info);die;
        }
    }


    /**
     * 还原求购
     */
    public function reductionSeek()
    {
        $sid = input('sid');

        $getSeek = db('seek',[],false)->where('sk_id',$sid)->find();

        if ($getSeek['sk_status'] !== 2) {
            $info['code']    = '400';
            $info['message'] = '该求购状态已正常，请刷新后查看';
            echo displayJson($info);die;
        }

        $set['sk_status']   = 1;
        $set['sk_del_aid']  = NULL;
        $set['sk_note']     = NULL;
        $set['sk_del_time'] = NULL;

        $res = db('seek',[],false)->where('sk_id',$sid)->update($set);

        if ($res) {
            //向用户发送邮件
            $getUser = db('user',[],false)->where('us_id',$getSeek['sk_uid'])->find();

            $send_data = array(
                'to_email'   =>$getUser['us_email'],
                'content'    =>'【淘二货】您发布的求购二货：<span style="font-size: 20px;font-weight: bold;"> '.$getSeek['sk_title'].' </span>，状态已回复正常。<br/>如有疑问，请联系管理员QQ：1298025852'
            );

            $send      = sendEmail($send_data);

            if (!$send) {
                $info['code']    = '400';
                $info['message'] = '更新成功,发送邮件失败';
                echo displayJson($info);die;
            } else {
                $info['code']    = '200';
                $info['message'] = '更新成功';
                echo displayJson($info);die;
            }
        } else {
            $info['code']    = '400';
            $info['message'] = '更新失败';
            echo displayJson($info);die;
        }
    }



}